/**
 * Created by Administrator on 2016/3/16 0016.
 */
angular.module('app.print', ['ui.router'])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider) {
        $stateProvider//print
            .state('home', {
                'abstract': true,
                url: '',
                views: {
                    '': {
                        templateUrl: "view/home.html"
                    }
                }
            }).state('home.first', {
                'abstract': true,
                url: '/home',
                views: {
                    'topbar': {
                        templateUrl: 'view/topbar.html'
                    }, 'main': {
                        templateUrl: 'view/home/print/sidebar.html'
                    }
                }
            })
            .state('home.first.print', {
                url: '/print',
                templateUrl: 'view/home/print/print.html',
                controller: 'PrintController'
            })
    }])
    .controller("PrintController", ['$scope', '$state', '$stateParams', '$interval', 'PrintService',
        function ($scope, $state, $stateParams, $interval, PrintService) {
            $scope.devid = {};
            // timeprefix
            var date = new Date();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var timeprefix = month.toString().concat(day.toString()).concat(hour.toString()).concat(minute.toString());
            //
            var i = 0;
            function printidproduce() {
                if (i > 9999) {
                    i = 0;
                }
                i++;
                if (i.length = 1) {
                    i = '000' + i;
                } else if (i.length = 2) {
                    i = '00' + i;
                }else if(i.length = 3){
                    i = '0' + i;
                }
                return timeprefix.concat(i);
            }
//
            $scope.devid.interval = 15;
            $scope.devid.route = 'short';
            //$scope.devid.title = 'test';
            $scope.devid.mode = 4;
            $scope.devid.ordermode = 1;
            $scope.devid.num = 0;
            $scope.devid.content = '\n名称         单价         数量\n******************************' +
                '\n海鲜套餐     15        1\n猪杂套餐     15        1\n******************************';
//
            $scope.alert_msg = "";
            $scope.enable_submit = false;
            $scope.close_alert = function () {
                $scope.alert_msg = "";
            }
//
            $scope.test_prepare = function () {
                if ($scope.devid.type == 1) {
                    $scope.devid.route = 'medium';
                } else if ($scope.devid.type == 2) {
                    $scope.devid.route = 'long';
                }
            }
//
            $scope.test_one = function () {
                if($scope.devid.devid==undefined){
                    $scope.alert_msg = "SN不能为空！";
                }else{
                    $scope.enable_submit=true;
                    $scope.test_prepare();
                    countprint();
                }

            }
            var stop;
            $scope.test = function () {
				if($scope.devid.devid==undefined){
                    $scope.alert_msg = "SN不能为空！";
                }else{
                    $scope.enable_submit=true;
                    //
					$scope.test_prepare();
					//
					if (angular.isDefined(stop)) {
						$interval.cancel(stop);
						stop = undefined;
						return;
					}
					countprint();
					if($scope.devid.num==0){
						stop = $interval(countprint, $scope.devid.interval * 1000, $scope.devid.num);
					}else if ($scope.devid.num - 1 > 0) {
						stop = $interval(countprint, $scope.devid.interval * 1000, $scope.devid.num - 1);
					}
                }
                
            }

            $scope.cancel = function () {
                i = 0;
                if (angular.isDefined(stop)) {
                    $interval.cancel(stop);
                    stop = undefined;
                }
            }
            function countprint() {
                $scope.devid.printid = printidproduce();
				$scope.devid.title = $scope.devid.printid;
                var d = new Date;
                $scope.devid.printtime = [d.getFullYear(), d.getMonth() + 1,
                        d.getDate()].join('-') + ' ' +
                    [d.getHours(),
                        d.getMinutes(),
                        d.getSeconds()].join(':');
                $scope.devid.printContent = printcontent_prepare();
                PrintService.print($scope.devid.route, $scope.devid, $scope.devid.printContent, function (data) {

                }, function (data) {

                })
            }
            //
            function printcontent_prepare() {
                var content = '测试订单打印\n打印编号：';
                content = content.concat($scope.devid.printid + '\n', "打印时间：" + $scope.devid.printtime,
                    "\n打印内容：" + $scope.devid.content);
                content = content.concat('\n打印结束\n\n\n\n');
                //console.log(content);
                return content;
            }
        }])
